package com.scau.hwadee.examination.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.scau.hwadee.examination.entity.Textbookadjustmentapplication;
import com.scau.hwadee.examination.service.TextbookService;
import com.scau.hwadee.examination.utils.JsonUtils;
import com.scau.hwadee.examination.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Objects;

/**
 * @author YHX
 * @date 2019/7/31 15:58
 * description
 */
@Slf4j
@Controller
@RequestMapping("textbook")
@Api(tags = "TextbookController",description = "教材相关模块")
public class TextbookController {

    @Autowired
    private TextbookService textbookService;

    @PostMapping("textbookAdjustmentByPlanningSection")
    @ApiOperation(value = "设置主考院校的教材调整的起止日期")
    @ApiImplicitParam(name = "jsonArray",dataType = "JSONArray",value = "前端传过来的json数组",required = true)
    public ResponseEntity<ResponseVo> adjustTextbook(@RequestBody String data){
        log.debug("设置主考院校的教材调整的起止日期");
        JSONArray jsonArray= Objects.requireNonNull(JsonUtils.JSONToObject(data, JSONObject.class)).getJSONArray("jsonArray");
        ResponseVo vo=ResponseVo.instance();
        try {
            int cnt=textbookService.adjustTextbook(jsonArray);
            String message;
            if (cnt>0){
                message="设置主考院校的教材调整的起止日期成功";
            }else {
                message="设置主考院校的教材调整的起止日期失败";
            }
            vo.setMessage(message);
        }catch (Exception e){
            vo.setMessage("设置主考院校的教材调整的起止日期失败");
        }
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @PostMapping("textbookAdjustmentBySchool")
    @ApiOperation(value = "主考院校填报教材调整申请")
    public ResponseEntity<ResponseVo> adjustTextbookBySchool(@RequestBody String data){
        ResponseVo vo=ResponseVo.instance();
        Textbookadjustmentapplication t=JsonUtils.JSONToObject(data,Textbookadjustmentapplication.class);
        int cnt=textbookService.adjustTextbookBySchool(t);
        String message;
        if (cnt>0){
            message="填报教材调整申请成功";
        }else {
            message="填报教材调整申请成功";
        }
        vo.setMessage(message);
        return new ResponseEntity<>(vo,HttpStatus.OK);
    }

}
